/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.ycl.wx.nrd.update.util;

import cn.ac.iie.di.datadock.rdata.exchange.client.exception.REConnectionException;
import cn.ac.iie.di.datadock.rdata.exchange.client.exception.RESessionException;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSession;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSessionBuilder;
import java.util.concurrent.LinkedBlockingQueue;

/**
 *
 * @author zwlin
 */
public class SenderPool {

	RESendSessionBuilder builder;
	LinkedBlockingQueue<RESendSession> pool;

	public SenderPool(RESendSessionBuilder builder) {
		this.builder = builder;
		this.pool = new LinkedBlockingQueue<>();
	}

	public RESendSession BorrowSession() throws RESessionException, REConnectionException {
		RESendSession rtn = pool.poll();
		if (rtn == null) {
			rtn = (RESendSession) builder.build();
			rtn.start();
		}
		return rtn;

	}

	public void ReturnSession(RESendSession session) throws InterruptedException {
		pool.put(session);
	}
}
